#include <bits/stdc++.h>
#define mod 999999999
#define ull unsigned long long
#define MAX 4010
using namespace std;
int t;
short n;
int x;
bool dir;
short lc, op;
int p, q;
int a[MAX][MAX];
short tp;
ull ans;
int main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    freopen("fire.in", "r", stdin);
    freopen("fire.out", "w", stdout);
    cin >> n >> t;
    while (t--)
    {
        cin >> tp;
        if (tp == 1)
        {
            cin >> dir >> lc >> op >> x;
            if (dir)
            { // 列操作
                switch (op)
                {
                case 1:
                {
                    for (int i = 1; i <= n; i++)
                        a[lc][i] &= x;
                    break;
                }
                case 2:
                {
                    for (int i = 1; i <= n; i++)
                        a[lc][i] |= x;
                    break;
                }
                case 3:
                {
                    for (int i = 1; i <= n; i++)
                        a[lc][i] ^= x;
                    break;
                }
                case 4:
                {
                    for (int i = 1; i <= n; i++)
                        a[lc][i] = (a[lc][i] + x) % mod;
                    break;
                }
                }
            }
            else
            { // 行操作
                switch (op)
                {
                case 1:
                {
                    for (int i = 1; i <= n; i++)
                        a[i][lc] &= x;
                    break;
                }
                case 2:
                {
                    for (int i = 1; i <= n; i++)
                        a[i][lc] |= x;
                    break;
                }
                case 3:
                {
                    for (int i = 1; i <= n; i++)
                        a[i][lc] ^= x;
                    break;
                }
                case 4:
                {
                    for (int i = 1; i <= n; i++)
                        a[i][lc] = (a[i][lc] + x) % mod;
                    break;
                }
                }
            }
        }
        else
        {
            ans = 0;
            cin >> p >> q;
            for (int i = 1; i <= q; i++)
                for (int f = 1; f <= p; f++)
                    ans += a[i][f];
            cout << ans << '\n';
        }
    }
}